package com.hyg.module_user.systemSetting;


import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hyg.lib_base.lisener.PermissionListener;
import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.constant.Constants;
import com.hyg.module_user.R;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

@Route(path = Constants.PATH_ACTIVITY_MODULE_USER_SETTING_ABOUTUS)
public class AboutUsActivity extends BaseActivity {

    private ImageView iv_tag;
    private ImageView back;
    private TextView app_version;
    private Button service_phone;
    private String ServicePhone = "02286393537";
    private ImageView iv_wx_gzh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 00, null, null, true);

        getPermissions(needPermissions, new PermissionListener() {
            @Override
            public void onResult(int[] results) {

            }
        });
        init();
    }

    public void init() {
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    return;
                }
                AboutUsActivity.this.finish();
                overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
            }
        });

        iv_wx_gzh = (ImageView) findViewById(R.id.iv_wx_gzh);

        iv_tag = (ImageView) findViewById(R.id.iv_tag);
        app_version = (TextView) findViewById(R.id.app_version);
        service_phone = (Button) findViewById(R.id.service_phone);
        app_version.setText("版本号:\u3000V" + getVersionName(this));
        SpannableString spannableString = new SpannableString("客服电话：\u3000022-8639 3537");
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#7b40be")), 6,
                spannableString.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        service_phone.setText(spannableString);
        service_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick())
                    return;
                call(ServicePhone, AboutUsActivity.this);
            }
        });
        iv_wx_gzh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) return;
                saveImage();
            }
        });
    }

    public void saveImage() {
        File wxFile = new File(checkDirPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/WX"), "HYG-WX-GZH.jpg");
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.wx_code);
        try {
            FileOutputStream out = new FileOutputStream(wxFile);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            out.write(stream.toByteArray());
            out.flush();
            out.close();
            //保存图片后发送广播通知更新数据库
            Uri uri = Uri.fromFile(wxFile);
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            showToast("已保存到本地：" + wxFile.getAbsolutePath());
//            pDialog = new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE);
//            pDialog.setTitleText("保存成功");
//            pDialog.setContentText("已保存到本地："+wxFile.getAbsolutePath());
//            pDialog.setConfirmButton("好的", new SweetAlertDialog.OnSweetClickListener() {
//                public void onClick(SweetAlertDialog sweetAlertDialog) {
//                     sweetAlertDialog.dismiss();
//                }
//            });
//            pDialog.show();

        } catch (Exception e) {
            showToast("保存失败，请您授权【读写文件权限】后重试。");
//            e.printStackTrace();
        }
    }

    /**
     * 检查文件是否存在
     */
    private static String checkDirPath(String dirPath) {
        if (TextUtils.isEmpty(dirPath)) {
            return "";
        }
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return dirPath;
    }

    //版本名
    public static String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    //版本号
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    //获取版本info
    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            Log.d(CrashTAG, "" + e.getMessage());
        }

        return pi;
    }

//    @SuppressLint("MissingPermission")
    //直接拨号功能
    public void call(String number, Context context) {
        if (!Utils.isEmpty(number)) {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Uri data = Uri.parse("tel:" + number);
            intent.setData(data);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{
                        Manifest.permission.READ_PHONE_STATE,Manifest.permission.CALL_PHONE}, 1);
//                return;
            }else{
                startActivity(intent);
            }
        }
    }

    /**
     * 需要进行检测的权限数组
     */
    protected String[] needPermissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CALL_PHONE
    };

}